/*
Is it feasible to instrument for copays?

Merge plan x drug copay information
- with manufacturer info (NDC)
- Construct a plan leave-one-out mean

*/



//////////////////////////////////////////
// 1st Approach: Same Drug, Other Plans //
//////////////////////////////////////////


///////////
// SETUP //
///////////


clear
gen year = 0
save temp, replace


forvalues j=1997/2013 {

display "////////////"
display "YEAR: `j'"
display "////////////"

cd "/disk/aging/mktscan/nongeo/data/100pct"
clear
set more off
use enrolid plankey genind ndcnum svcdate copay coins pay awp qty daysupp using ccaed`j'

keep if plankey != .

// mark cholesterol drugs
merge m:1 ndcnum using "compressed_redbook_w_indications", keepus(indic_id xr) keep(mat)
drop _merge

keep if indic_id == 5

gen cost_sharing = copay + coins

merge m:1 ndcnum using "redbook.dta", keepus(prodnme gennme strngth genind) keep(3)
drop _merge

merge m:1 gennme strngth using "strngth_cholesterol_drugs"
keep if _merge == 3
drop _merge

keep if class == 2

destring genind, replace force
keep if genind >= 1 & genind <= 5
gen generic = (genind == 4) | (genind == 5)


// compute median for focal plan, also leave-one-out mean of other plans
collapse (sum) qty (p50) cost_sharing, by(plankey gennme generic xr)

egen t = tag(plankey)
egen total_plans = sum(t)
egen s = sum(cost_sharing), by(gennme generic xr)

gen loo_cost_sharing = (s - cost_sharing) / (total_plans-1)

//areg cost_sharing loo_cost_sharing, absorb(plankey)
//areg cost_sharing loo_cost_sharing if generic == 0, absorb(plankey) // relationship for branded drugs
gen year = `j'

append using temp
save temp, replace

}




egen py = group(plankey year)

//reghdfe cost_sharing1 cost_sharing0, absorb(py manufacturer)
reghdfe cost_sharing loo_cost_sharing if generic == 0, absorb(py, savefe) // PANEL C in Table 8


matrix coeff = e(b)
gen beta = coeff[1,1]

gen pred_copay = beta * loo_cost_sharing + __hdfe1__

keep plankey year gennme generic xr pred_copay

save loo_copay_instrument_predictions, replace




//////////////////////////////////////////////
// 2nd Approach: Manufacturers' Other Drugs //
//////////////////////////////////////////////



/*
Looking for manufacturer x plan residuals
*/


clear
gen year = 0
save temp, replace

///////////
// SETUP //
///////////


forvalues j=1997/2002 {

display "////////////"
display "YEAR: `j'"
display "////////////"

cd "/disk/aging/mktscan/nongeo/data/100pct"
clear
set more off
use enrolid plankey genind ndcnum svcdate copay coins pay awp qty daysupp using ccaed`j'

keep if plankey != .

gen cost_sharing = copay + coins



merge m:1 ndcnum using "redbook.dta", keepus(prodnme gennme manfnme genind) keep(3)
drop _merge

destring genind, replace force
keep if genind >= 1 & genind <= 5
gen generic = (genind == 4) | (genind == 5)

// mark cholesterol drugs
merge m:1 ndcnum using "compressed_redbook_w_indications", keepus(indic_id) keep(mat)
drop _merge

gen cholesterol_drug = (indic == 5)


// some manufacturers make both generic and branded
egen manufacturer = group(manfnme generic)

collapse (sum) qty (p50) cost_sharing (first) generic manfnme, by(plankey cholesterol_drug manufacturer)
egen man_plan = group(manufacturer plankey)

reshape wide cost_sharing qty, i(man_plan) j(cholesterol_drug) // indicator for cholesterol market

keep if cost_sharing1 != . // Merck, BMS, Novartis

// First stage: predict copay using copays on other drugs (within a plan)
// copay_ij = alpha + beta * copay_-i,j + Plan FE + Company FE

gen year = `j'

append using temp
save temp, replace

}

egen py = group(plankey year)


//reghdfe cost_sharing1 cost_sharing0, absorb(py manufacturer)
reghdfe cost_sharing1 cost_sharing0 if generic == 0, absorb(py manufacturer, savefe) // PANEL B in Table 8


matrix coeff = e(b)
gen beta = coeff[1,1]

gen pred_copay = beta * cost_sharing0 + __hdfe1__ + __hdfe2__

keep manfnme plankey year pred_copay

save manufacturer_predicted_copay_weighted, replace










////////////////////////
// UNWEIGHTED VERSION //
////////////////////////

clear
gen year = 0
save temp, replace



forvalues j=1997/2002 {

display "////////////"
display "YEAR: `j'"
display "////////////"

clear
set more off
use enrolid plankey genind ndcnum svcdate copay coins pay awp qty daysupp using ccaed`j'

keep if plankey != .

gen cost_sharing = copay + coins



merge m:1 ndcnum using "redbook.dta", keepus(prodnme gennme manfnme genind) keep(3)
drop _merge

destring genind, replace force
keep if genind >= 1 & genind <= 5
gen generic = (genind == 4) | (genind == 5)

// mark cholesterol drugs
merge m:1 ndcnum using "compressed_redbook_w_indications", keepus(indic_id) keep(mat)
drop _merge

gen cholesterol_drug = (indic == 5)


// some manufacturers make both generic and branded
egen manufacturer = group(manfnme generic)
collapse (p50) cost_sharing (first) generic cholesterol_drug manfnme, by(plankey ndcnum manufacturer) // collapse to drug x plan level => median copay for each drug
collapse (mean) cost_sharing (first) generic manfnme, by(plankey cholesterol_drug manufacturer) // average across drugs (unweighted)
egen man_plan = group(manufacturer plankey)

reshape wide cost_sharing, i(man_plan) j(cholesterol_drug)

keep if cost_sharing1 != . // Merck, BMS, Novartis

gen year = `j'

append using temp
save temp, replace


}




egen py = group(plankey year)


//reghdfe cost_sharing1 cost_sharing0, absorb(py manufacturer)
reghdfe cost_sharing1 cost_sharing0 if generic == 0, absorb(py manufacturer, savefe)


matrix coeff = e(b)
gen beta = coeff[1,1]

gen pred_copay = beta * cost_sharing0 + __hdfe1__ + __hdfe2__

keep manfnme plankey year pred_copay

save manufacturer_predicted_copay_unweighted, replace



















